package com.bee.剑指offer;

import java.util.Vector;

/**
 * 输入："nowcoder. a am I"
 * 返回值："I am a nowcoder."
 */
public class 翻转单词序列 {

    /**
     * 方法：字符串截断方法，将一个个单词截取出来
     *
     * @param str
     * @return
     */
    public static String ReverseSentence(String str) {
        Vector<String> vector = new Vector<>();
        // -1 if there is no such occurrence.
        int index = 0;

        // 找不到" "字符串，单词全部切割完成
        while (index != -1) {
            index = str.indexOf(" ");
            if (index == -1) {
                break;
            }
            String substring = str.substring(0, index);
            vector.add(str.substring(0, index));
            str = str.substring(index + 1);
        }
        vector.add(str);
        String result = "";
        // 倒序输出
        for (int i = vector.size() - 1; i >= 0; i--) {
            result += vector.get(i);
            result += " ";
        }
        String trim = result.trim();
        return trim;
    }

    public static void main(String[] args) {
        String str = ReverseSentence("nowcoder. a am I");
        System.out.println(str);
    }
}
